// pages/index/fabricChoice/fabricChoice.js
const app = getApp();
const util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fabricList: [],
    fabricColor: [],
    productId: ""
  },

  // 选择对应目标
  selectFabric: function(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    for (let i = 0; i < that.data.fabricList.length; i++) {
      if (index == i) {
        that.data.fabricList[i].select = false
      } else {
        that.data.fabricList[i].select = true
      }
      this.setData(this.data)
    }
    // console.log(this.data.fabricColor)
  },

  next: function (e) {
    let that = this
    let list = that.data.fabricList
    for (let i = 0; i < list.length; i++) {
      if(list[i].select == false) {
        let color = {
          paramName: "面料",
          option: list[i].name
        }
        that.data.fabricColor.push(color)
      }
    }
    that.setData(that.data)
    // console.log(that.data.fabricColor)
    // 将面料信息传回后台
    let msgUrl = app.globalData.baseUrl + "weixin/saveMsgTemp"
    let data = {
      cid: wx.getStorageSync('cid'),
      msg: that.data.fabricColor,
      type: 1
    }
    util.requestLoading(msgUrl, data, "GET").then(res => {
      // console.log(res)
      wx.navigateTo({
        url: '../customOptions/customOptions?productId=' + that.data.productId
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    that.setData({
      productId: options.productId
    })
    // console.log(that.data.productId)
    // 请求面料列表
    let fabricUrl = app.globalData.baseUrl + "weixin/fabricList"
    util.requestLoading(fabricUrl, null, "GET").then(res => {
      // console.log(res)
      this.setData({
        fabricList: res.data.fabricList
      })
    })
  },
})